package com.pillar.handler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.pillar.enty.Room;
import com.pillar.game.BizHandler;
import com.pillar.game.Cmd;
import com.pillar.request.BasePackage;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;


/**
 * @author pillar
 * @desc
 * @email: 347637454@qq.com
 * @createDate 2021-01-04 15:49
 */
@Slf4j
public class JoinRoomHandler extends AbstractCheckLoginHandler {
    @Override
    public boolean apply(BasePackage basePackage) {
        return Cmd.JOIN_ROOM.equals(basePackage.getCmd());
    }

    @Override
    public boolean consume(Channel channel, BasePackage basePackage) throws JsonProcessingException {
        super.consume(channel, basePackage);
        log.info("玩家加入房间={}",basePackage);
        Room room = null;
        if (null != (room = BizHandler.hall.findRoomByUuid(basePackage.getUuid()))){
            writeText(channel,"玩家已经存在房间中，房间ID="+room.getRoomId());
            return false;
        }
        room = BizHandler.hall.findRoom(basePackage.getData());
        if (null == room){
            writeText(channel,"房间不存在");
            return false;
        }
        if (room.fullPlayer()){
            writeText(channel,"房间已满");
            return false;
        }
        BizHandler.hall.join(basePackage.getUuid(),room);
        return false;
    }
}
